package com.lzw.okgoutils.sample;

import com.lzw.okgoutils.http.BaseHttpRequest;
import com.lzw.okgoutils.http.HttpCallback;
import com.lzw.okgoutils.http.HttpClient;
import com.lzw.okgoutils.http.RequestParams;
import com.lzw.okgoutils.http.otherutils.BaseConstant;

import java.util.List;

/**
 *
 */
public class LoginAction implements ILoginAction{

    private static LoginAction mLoginAction = null;
    private BaseConstant mApi;

    private LoginAction(){}

    public static LoginAction getInstance() {
        synchronized (LoginAction.class) {
            if (mLoginAction == null) {
                mLoginAction = new LoginAction();
            }
        }
        return mLoginAction;
    }

    @Override
    public void cancelAction(List<Integer> tags) {
        HttpClient.getInstance().cancelRequest(tags);
    }

    @Override
    public String setApi(BaseConstant api, int tag) {
        this.mApi = api;
        if(tag == BaseConstant.TAG_URL_LOGIN_IN){
            return BaseConstant.getLoginUrl();
        }else if(tag == BaseConstant.TAG_URL_LOGIN_OUT){
            return BaseConstant.getLoginOutUrl();
        }
        return null;
    }

    @Override
    public void loginIn(String actionName, RequestParams params, HttpCallback callback, int tag) {
        String url = setApi(mApi,BaseConstant.TAG_URL_LOGIN_IN);
        BaseHttpRequest request = new BaseHttpRequest(url, params, callback, tag);
        //这里用的是get请求，如果项目用的是post，在这里换成post就好了
        //具体的请查看HttpClient类的介绍
        HttpClient.getInstance().get(request);
    }

    @Override
    public void loginOut(String actionName, RequestParams params, HttpCallback callback, int tag) {
        String url = setApi(mApi,BaseConstant.TAG_URL_LOGIN_OUT);
        BaseHttpRequest request = new BaseHttpRequest(url, params, callback, tag);
        HttpClient.getInstance().get(request);
    }
}
